//
//  LargeImageViewController.m
//  dingding
//
//  Created by LHL on 14/12/19.
//  Copyright (c) 2014年 com.gordon. All rights reserved.
//

#import "LargeImageViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "HYCircleLoadingView.h"
@interface LargeImageViewController ()<UIScrollViewDelegate>

@end

@implementation LargeImageViewController{
    UIScrollView *scroll;
    SHAREITEMMODEL *sharemodel;
    CGFloat preX;
    BOOL isfirst;
    UILabel *current;
}

- (id)init {
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        self.backgroundColor = [UIColor blackColor];
        scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, currentScreen.width, currentScreen.height-44-64)];
        scroll.showsHorizontalScrollIndicator = NO;
        scroll.showsVerticalScrollIndicator = NO;
        scroll.bounces = NO;
        scroll.delegate = self;
        scroll.pagingEnabled = YES;
        [self addSubview:scroll];
        isfirst = YES;
        for (int i = 0; i < 9; i++) {
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(scroll.frame.size.width*i, 0, scroll.frame.size.width, scroll.frame.size.height)];
            img.tag = 11200+i;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeself)];
            img.userInteractionEnabled = YES;
            [img addGestureRecognizer:tap];
            [scroll addSubview:img];
            HYCircleLoadingView *circle = [[HYCircleLoadingView alloc]init];
            circle.tag = 11250+i;
            circle.lineColor = [UIColor whiteColor];
            [scroll addSubview:circle];
            UILabel *label = [[UILabel alloc]init];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:12];
            label.textColor = [UIColor whiteColor];
            label.tag = 11275+i;
            [scroll addSubview:label];
            
        }
        current = [[UILabel alloc]initWithFrame:CGRectMake(0, currentScreen.height-15, currentScreen.width, 29)];
        current.textColor= [UIColor whiteColor];
        current.font = [UIFont systemFontOfSize:14];
        current.textAlignment = NSTextAlignmentCenter;
        [self addSubview:current];
    }
    return self;
}

-(void)removeself{
    [self removeFromSuperview];
}

-(void)setimageCount:(SHAREITEMMODEL *)model andCurrentIndex:(NSInteger)num{
    sharemodel = model;
    scroll.contentSize = CGSizeMake(model.image.count*[UIScreen mainScreen].bounds.size.width, 0);
    scroll.contentOffset = CGPointMake(num*currentScreen.width, 0);
    [self scrollViewDidEndDecelerating:scroll];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    int x = scroll.contentOffset.x/currentScreen.width;
    __weak UIImageView *img = (UIImageView *)[scroll viewWithTag:x+11200];
    current.text = [NSString stringWithFormat:@"%d / %d",x+1,sharemodel.image.count];
    IMAGEMODEL *imgmodel = sharemodel.image[x];
    
    HYCircleLoadingView *circle = (HYCircleLoadingView *)[scroll viewWithTag:11250+x];
    CGRect frame = img.frame;
    frame.origin.x = img.frame.origin.x + currentScreen.width/2 - currentScreen.width*0.10/2;
    frame.origin.y = img.frame.size.height/2 - currentScreen.width*0.10/2;
    frame.size = CGSizeMake(currentScreen.width*0.10, currentScreen.width*0.10);
    circle.frame = frame;
    
   __weak UILabel *label = (UILabel *)[scroll viewWithTag:11275+x];
    label.frame = circle.frame;
    label.hidden = NO;
    
    [img setImageWithURL:[NSURL URLWithString:imgmodel.largeimg] placeholderImage:[UIImage imageNamed:@"black_back.png"] options:SDWebImageLowPriority progress:^(NSInteger recievedSize,NSInteger expectedSize){
        [circle startAnimation];
        CGFloat rec = [[NSString stringWithFormat:@"%d",recievedSize]floatValue];
        CGFloat exp = [[NSString stringWithFormat:@"%d",expectedSize]floatValue];
        label.text = [NSString stringWithFormat:@"%.0f",rec/exp*100];
    }completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
        [circle stopAnimation];
        label.hidden = YES;
    }];
    
}

//-(void)setimageCount:(SHAREITEMMODEL *)model andCurrentIndex:(NSInteger)num{
//    sharemodel = model;
//    scroll.contentSize = CGSizeMake(model.image.count*[UIScreen mainScreen].bounds.size.width, 0);
//    scroll.contentOffset = CGPointMake(num*currentScreen.width, 0);
//        for (int i = 0; i < 3; i++) {
//            UIImageView *img = (UIImageView *)[scroll viewWithTag:11200+i];
//            img.frame = CGRectMake(currentScreen.width*(i+num-1), 0, currentScreen.width, currentScreen.height);
//        }
//    [self scrollViewDidEndDecelerating:scroll];
//    
//}
//
//#pragma mark - ScrollViewDelegate
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    int x = scroll.contentOffset.x/currentScreen.width;
//    __weak UIImageView *img = (UIImageView *)[scroll viewWithTag:11201];
//    if (isfirst) {
//        //预加载
//        __weak  UIImageView *cleanImg = (UIImageView *)[scroll viewWithTag:11202];
//        __weak  UIImageView *imgpre = (UIImageView *)[scroll viewWithTag:11200];
//        if (x+1 < sharemodel.image.count) {
//            IMAGEMODEL *imgmodel = sharemodel.image[x+1];
//            [cleanImg setImageWithURL:[NSURL URLWithString:imgmodel.largeimg] placeholderImage:[UIImage imageNamed:@"photo.png"] options:SDWebImageLowPriority progress:^(NSInteger recievedSize,NSInteger expectedSize){
//                
//            }completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
//                cleanImg.image = image;
//            }];
//        }
//        
//        if (x - 1 >= 0) {
//            IMAGEMODEL *imgmodel = sharemodel.image[x-1];
//            [imgpre setImageWithURL:[NSURL URLWithString:imgmodel.largeimg] placeholderImage:[UIImage imageNamed:@"photo.png"] options:SDWebImageLowPriority progress:^(NSInteger recievedSize,NSInteger expectedSize){
//                
//            }completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
//                imgpre.image = image;
//            }];
//        }
//        isfirst = NO;
//    }else{
//        if (x %3 == 1) {
//            if (preX < scroll.contentOffset.x) {
//                //之后的
//                img = (UIImageView *)[scroll viewWithTag:11202];
//                
//                __weak  UIImageView *imgpre = (UIImageView *)[scroll viewWithTag:11201];
//                __weak  UIImageView *cleanImg = (UIImageView *)[scroll viewWithTag:11200];
//                CGRect frame = imgpre.frame;
//                frame.origin.x = scroll.contentOffset.x + currentScreen.width;
//                imgpre.frame = frame;
//                
//                //预加载
////                if (x+1 < sharemodel.image.count) {
////                    IMAGEMODEL *imgmodel = sharemodel.image[x+1];
////                    [cleanImg setImageWithURL:[NSURL URLWithString:imgmodel.largeimg] placeholderImage:[UIImage imageNamed:@"photo.png"] options:SDWebImageLowPriority progress:^(NSInteger recievedSize,NSInteger expectedSize){
////                        
////                    }completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
////                        cleanImg.image = image;
////                    }];
////                }
////                
////                if (x - 1 >= 0) {
////                    IMAGEMODEL *imgmodel = sharemodel.image[x-1];
////                    [imgpre setImageWithURL:[NSURL URLWithString:imgmodel.largeimg] placeholderImage:[UIImage imageNamed:@"photo.png"] options:SDWebImageLowPriority progress:^(NSInteger recievedSize,NSInteger expectedSize){
////                        
////                    }completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
////                        imgpre.image = image;
////                    }];
////                }
//            }else{
//                //之前的
//                img = (UIImageView *)[scroll viewWithTag:11200];
//                __weak  UIImageView *imgpre= (UIImageView *)[scroll viewWithTag:11202];
//                __weak  UIImageView *cleanImg= (UIImageView *)[scroll viewWithTag:11201];
//                CGRect framepre = imgpre.frame;
//                framepre.origin.x = scroll.contentOffset.x - currentScreen.width;
//                imgpre.frame = framepre;
//                
//                //预加载
////                if (x+1 < sharemodel.image.count) {
////                    IMAGEMODEL *imgmodel = sharemodel.image[x+1];
////                    [cleanImg setImageWithURL:[NSURL URLWithString:imgmodel.largeimg] placeholderImage:[UIImage imageNamed:@"photo.png"] options:SDWebImageLowPriority progress:^(NSInteger recievedSize,NSInteger expectedSize){
////                        
////                    }completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
////                        cleanImg.image = image;
////                    }];
////                }
////                
////                if (x - 1 >= 0) {
////                    IMAGEMODEL *imgmodel = sharemodel.image[x-1];
////                    [imgpre setImageWithURL:[NSURL URLWithString:imgmodel.largeimg] placeholderImage:[UIImage imageNamed:@"photo.png"] options:SDWebImageLowPriority progress:^(NSInteger recievedSize,NSInteger expectedSize){
////                        
////                    }completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
////                        imgpre.image = image;
////                    }];
////                }
//            }
//        }
//        if (x %3 == 2) {
//            if (preX < scroll.contentOffset.x) {
//                //之后的
//                img = (UIImageView *)[scroll viewWithTag:11200];
//                
//                __weak UIImageView *imgpre = (UIImageView *)[scroll viewWithTag:11202];
//                __weak UIImageView *cleanImg = (UIImageView *)[scroll viewWithTag:11201];
//                CGRect frame = imgpre.frame;
//                frame.origin.x = scroll.contentOffset.x + currentScreen.width;
//                imgpre.frame = frame;
//                
//                //预加载
////                if (x+1 < sharemodel.image.count) {
////                    IMAGEMODEL *imgmodel = sharemodel.image[x+1];
////                    [cleanImg setImageWithURL:[NSURL URLWithString:imgmodel.largeimg] placeholderImage:[UIImage imageNamed:@"photo.png"] options:SDWebImageLowPriority progress:^(NSInteger recievedSize,NSInteger expectedSize){
////                        
////                    }completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
////                        cleanImg.image = image;
////                    }];
////                }
////                
////                if (x - 1 >= 0) {
////                    IMAGEMODEL *imgmodel = sharemodel.image[x-1];
////                    [imgpre setImageWithURL:[NSURL URLWithString:imgmodel.largeimg] placeholderImage:[UIImage imageNamed:@"photo.png"] options:SDWebImageLowPriority progress:^(NSInteger recievedSize,NSInteger expectedSize){
////                        
////                    }completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
////                        imgpre.image = image;
////                    }];
////                }
//            }else{
//                //之前的
//                img = (UIImageView *)[scroll viewWithTag:11202];
//                
//                __weak  UIImageView *cleanImg = (UIImageView *)[scroll viewWithTag:11200];
//                __weak  UIImageView *imgpre = (UIImageView *)[scroll viewWithTag:11201];
//                CGRect framepre = imgpre.frame;
//                framepre.origin.x = scroll.contentOffset.x - currentScreen.width;
//                imgpre.frame = framepre;
//                
//                //预加载
////                if (x+1 < sharemodel.image.count) {
////                    IMAGEMODEL *imgmodel = sharemodel.image[x+1];
////                    [cleanImg setImageWithURL:[NSURL URLWithString:imgmodel.largeimg] placeholderImage:[UIImage imageNamed:@"photo.png"] options:SDWebImageLowPriority progress:^(NSInteger recievedSize,NSInteger expectedSize){
////                        
////                    }completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
////                        cleanImg.image = image;
////                    }];
////                }
////                
////                if (x - 1 >= 0) {
////                    IMAGEMODEL *imgmodel = sharemodel.image[x-1];
////                    [imgpre setImageWithURL:[NSURL URLWithString:imgmodel.largeimg] placeholderImage:[UIImage imageNamed:@"photo.png"] options:SDWebImageLowPriority progress:^(NSInteger recievedSize,NSInteger expectedSize){
////                        
////                    }completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
////                        imgpre.image = image;
////                    }];
////                }
//            }
//        }
//        if (x % 3 == 0) {
//            if (preX < scroll.contentOffset.x) {
//                //之后的
//                __weak UIImageView *cleanImg = (UIImageView *)[scroll viewWithTag:11202];
//                img = (UIImageView *)[scroll viewWithTag:11201];
//                
//                __weak UIImageView * imgpre = (UIImageView *)[scroll viewWithTag:11200];
//                CGRect frame = imgpre.frame;
//                frame.origin.x = scroll.contentOffset.x+currentScreen.width;
//                imgpre.frame = frame;
//                
////                if (x+1 < sharemodel.image.count) {
////                    IMAGEMODEL *imgmodel = sharemodel.image[x+1];
////                    [cleanImg setImageWithURL:[NSURL URLWithString:imgmodel.largeimg] placeholderImage:[UIImage imageNamed:@"photo.png"] options:SDWebImageLowPriority progress:^(NSInteger recievedSize,NSInteger expectedSize){
////                        
////                    }completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
////                        cleanImg.image = image;
////                    }];
////                }
////                
////                if (x - 1 >= 0) {
////                    IMAGEMODEL *imgmodel = sharemodel.image[x-1];
////                    [imgpre setImageWithURL:[NSURL URLWithString:imgmodel.largeimg] placeholderImage:[UIImage imageNamed:@"photo.png"] options:SDWebImageLowPriority progress:^(NSInteger recievedSize,NSInteger expectedSize){
////                        
////                    }completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
////                        imgpre.image = image;
////                    }];
////                }
//                
//            }else{
//                //之前的
//                img = (UIImageView *)[scroll viewWithTag:11200];
//                __weak UIImageView *cleanImg = (UIImageView *)[scroll viewWithTag:11202];
//                __weak UIImageView *imgpre = (UIImageView *)[scroll viewWithTag:11201];
//                
//                CGRect framepre = imgpre.frame;
//                framepre.origin.x = scroll.contentOffset.x-currentScreen.width;
//                imgpre.frame = framepre;
//                
////                if (x+1 < sharemodel.image.count) {
////                    IMAGEMODEL *imgmodel = sharemodel.image[x+1];
////                    [cleanImg setImageWithURL:[NSURL URLWithString:imgmodel.largeimg] placeholderImage:[UIImage imageNamed:@"photo.png"] options:SDWebImageLowPriority progress:^(NSInteger recievedSize,NSInteger expectedSize){
////                        
////                    }completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
////                        cleanImg.image = image;
////                    }];
////                }
////                
////                if (x - 1 >= 0) {
////                    IMAGEMODEL *imgmodel = sharemodel.image[x-1];
////                    [imgpre setImageWithURL:[NSURL URLWithString:imgmodel.largeimg] placeholderImage:[UIImage imageNamed:@"photo.png"] options:SDWebImageLowPriority progress:^(NSInteger recievedSize,NSInteger expectedSize){
////                        
////                    }completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
////                        imgpre.image = image;
////                    }];
////                }
//            }
//        }
//    }
//    IMAGEMODEL *imgmodel = sharemodel.image[x];
//    [img setImageWithURL:[NSURL URLWithString:imgmodel.largeimg] placeholderImage:[UIImage imageNamed:@"photo.png"] options:SDWebImageLowPriority progress:^(NSInteger recievedSize,NSInteger expectedSize){
//        
//    }completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType){
//            img.image = image;
//        NSLog(@"123");
//    }];
//    preX = scroll.contentOffset.x;
//}


@end
